package com.max.mice.orm.dbutil;

import java.beans.PropertyVetoException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Main 
{
	public static void main(String[] args) throws Exception 
	{
		DataSource dataSource = getDataSource("jdbc:mysql://192.168.110.111/maxchen","root","");
		
	    QueryRunner run = new QueryRunner(dataSource);
	    
	    /**增*/
	    int i = run.update("INSERT INTO t_project_graphics_content_tmp SET content='111',creator='system',modifier='system',create_date='2013-01-01',modify_date='2013-01-01'");
	    System.out.println(i);
	    
	    /**删*/
	    i = run.update("DELETE FROM t_project_graphics_content_tmp WHERE id=?","1");
	    System.out.println(i);
	    
	    /**改*/
	    i = run.update("UPDATE t_project_graphics_content_tmp SET content='999' WHERE content=?","111");
	    System.out.println(i);
	    
	    /**查*/
	    Content content = (Content) run.query("select * from t_project_graphics_content_tmp where id=10", new BeanHandler(Content.class));
	    System.out.println(content.getId());
	    List<Content> list = (List<Content>)run.query("select * from t_project_graphics_content_tmp", new BeanListHandler(Content.class));
	    System.out.println(list.size());
	}
	
	
	
	public static DataSource getDataSource(String jdbcUrl, String username, String password) 
		throws PropertyVetoException
	{
		ComboPooledDataSource cpds = new ComboPooledDataSource();
	    cpds.setDriverClass("org.gjt.mm.mysql.Driver");
	    cpds.setJdbcUrl(jdbcUrl);
	    cpds.setUser(username);
	    cpds.setPassword(password);
	    cpds.setMaxStatements(180);
	    cpds.setMaxPoolSize(100);
	    
	    return cpds;
	}
}
